package cn.hx.mall.permissions.controller;

import cn.hx.mall.permissions.entity.Admin;
import cn.hx.mall.permissions.entity.Role;
import cn.hx.mall.permissions.entity.page.Page;
import cn.hx.mall.permissions.service.AdminService;
import cn.hx.mall.permissions.service.RoleService;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/permissions")
public class AdminController {

    @Resource
    private AdminService adminService;
    @Resource
    private RoleService roleService;


    @GetMapping("/admin")
    public String findAll(Model model, Page page) {
        if (page.getPage() == null || page.getPage() < 1) {
            page.setPage(1);
        }
        if (page.getLimit() == null) {
            page.setLimit(10);
        }
        Admin admin = new Admin();
        admin.setUsername(page.getSearch());
        admin.setNickName(page.getSearch());
//        System.out.println(admin);
        List<Admin> all = adminService.findAll(admin);
        Page page1 = adminService.getPageInfo(page, admin);
        model.addAttribute("adminList", all);
        model.addAttribute("pageInfo", page1);
        return "permissions/admin";
    }

    @PostMapping("/admin")
    public String admin(Model model, String str) {
        List<Admin> query = adminService.query(str);
        model.addAttribute("adminList", query);
        return "permissions/admin";
    }

    @SuppressWarnings("unused")
    @GetMapping("/appendAdmin")
    public String myAppendAdmin(Long id, Model model) {
        return "permissions/appendAdmin";
    }

    @GetMapping("/editAdmin")
    public String editAdmin(Long id, Model model) {

        Admin admin = adminService.queryById(id);
        model.addAttribute("admin", admin);
        return "permissions/appendAdmin";
    }

    @PostMapping("/editAdmin")
    public String myEditAdmin(Admin admin) {
        Admin update = adminService.update(admin);
        System.out.println(update);
        return "redirect:/permissions/admin";
    }

    @GetMapping("/deleteAdmin/{id}")
    public String myDeleteAdmin(@PathVariable Long id) {
        boolean b = adminService.deleteById(id);
        if (b) {
            System.out.println("删除成功");
        }
        return "redirect:/permissions/admin";
    }

    @PostMapping("/appendAdmin")
    public String myAppendAdmin(Admin admin) {
        System.out.println(admin);
        admin.setCreateTime(new Date());
        if (admin.getId() != null) {
            Admin update = adminService.update(admin);
            System.out.println(update);
        } else {
            Admin insert = adminService.insert(admin);
            System.out.println(insert);
        }
        return "redirect:/permissions/admin";
    }

    @GetMapping("/deleteAdmin")
    public String myDeleteAdmin() {

        return "redirect:/permissions/admin";
    }

    @GetMapping("admin/updateStatus/{id}")
    public String updateStatus(@PathVariable Long id) {
        Admin admin = adminService.queryById(id);
        if (admin.getStatus() == 1) {
            admin.setStatus(0);
        } else {
            admin.setStatus(1);
        }
        adminService.update(admin);
        return "redirect:/permissions/admin";
    }

    @GetMapping("/allocRole/{id}")
    public String myAllocRole(Model model,@PathVariable("id") Integer id) {
        List<Role> roleList = roleService.findAll();
        model.addAttribute("roleList", roleList);
        System.out.println(roleList);
        model.addAttribute("adminId", id);
        return "/permissions/allocRole";
    }

    @SuppressWarnings("unchecked")
    @PostMapping("/saveAllocRoles")
    @Transactional
    @ResponseBody
    public String saveAllocRoles(String ids, Integer adminId) {
        System.out.println(111);
        JSONObject id = JSONObject.parseObject(ids);
        List<String> ids1 = (List<String>) id.get("ids");
        adminService.deleteResourcesByAdminId(adminId);
        adminService.allocRoles(adminId, ids1);
        return "200";
    }
}